package org.zjt.algorithm;

/**
 * 链表反转：
 *  1、头插法
 *  2、当前节点的下一个节点放到头节点。
 */
public class LinkReverse {

    protected static class Node {
        private Node next;
        private Integer val;

        public Node(Node next, Integer val) {
            this.next = next;
            this.val = val;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        public Integer getVal() {
            return val;
        }

        public void setVal(Integer val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "next=" + next +
                    ", val=" + val +
                    '}';
        }
    }


    public static void main(String[] args) {
        Node head = new Node(new Node(new Node(new Node(new Node(null,4),3),2),1),null);
        if (head == null || head.next == null || head.next.next == null){
            return;
        }
        Node node = head.next;
        while (node != null){
            Node next = node.next;
            Node next1 = head.next;
            if (next == null){
                break;
            }
            node.next = next.next;
            head.next = next;
            next.next = next1;

            node = node.next;
        }
        Node tmp = head;
        while (tmp != null){
            System.out.println(tmp.val);
            tmp = tmp.next;
        }
    }

    public static void HeadInsert(String[] args) {
        Node head = new Node(new Node(new Node(new Node(new Node(null,4),3),2),1),null);


        Node head2 = new Node(null,null);
        Node tmp = head;
        while (tmp != null){
            Node next1 = tmp.next;
            Node next = head2.next;

            head2.next = tmp;
            tmp.next = next;
            tmp = next1;
        }

        tmp = head2;
        while (tmp != null){
            System.out.println(tmp.val);
            tmp = tmp.next;
        }

    }
}
